package cn.itcast.service.impl;

import cn.itcast.dao.UserDao;
import cn.itcast.dao.impl.UserDaoImpl;
import cn.itcast.pojo.AdminUser;
import cn.itcast.pojo.PageBean;
import cn.itcast.pojo.User;
import cn.itcast.service.UserService;

import java.util.List;
import java.util.Map;

/**
 * @author skycat
 * @time 2021/12/9
 */
public class UserServiceImpl implements UserService {
    private UserDao dao = new UserDaoImpl();

    @Override
    public List<User> findAll() {
        return dao.findAll();
    }

    public AdminUser findAdminUser(AdminUser user) {
        AdminUser adminUser = dao.findAdminUserByUserNameAndPassword(user);
        return adminUser;
    }

    @Override
    public void addUser(User user) {
        dao.addUser(user);
    }

    @Override
    public void deleteUserById(int id) {
        dao.deleteUserById(id);
    }

    @Override
    public User findUserById(int id) {
        return dao.findUserById(id);
    }

    @Override
    public void updateUser(User user) {
        dao.updateUser(user);
    }

    @Override
    public void delSelectedUsers(String[] uids) {
        if (uids != null && uids.length > 0) {
            for (String uid : uids) {
                dao.deleteUserById(Integer.parseInt(uid));
            }
        }
    }

    @Override
    public PageBean<User> findUserByPage(String currentPageStr, String rowsStr, Map<String, String[]> condition) {
        int currentPage = Integer.parseInt(currentPageStr);
        int rows = Integer.parseInt(rowsStr);
        PageBean<User> pageBean = new PageBean<>();
        pageBean.setCurrentPage(currentPage);
        pageBean.setRows(rows);
        int totalCount = dao.findUserCount(condition);
        pageBean.setTotalCount(totalCount);
        int totalPage=totalCount%rows==0?(totalCount/rows):(totalCount/rows+1);
        pageBean.setTotalPage(totalPage);
        int start=(currentPage-1)*rows;
        List<User> list=dao.findUserListByPage(start,rows,condition);
        pageBean.setList(list);
        return pageBean;
    }
}
